/**
 * Created by tanbingqin on 2020/11/6.
 */
import React from "react";
import moment from "moment";
import { message } from "antd";
import { messages, uniqueArr, queryCurrencyPrecision } from "share/common";

export default {
  /**
   * 处理table子表格控件的行数，数据
   * @param field 子表格控件
   */
  processRowList(field) {
    let rowList = [];
    if (field.showValue && JSON.parse(field.showValue) && JSON.parse(field.showValue).length) {
      let tempRowList = JSON.parse(field.showValue);
      let oneRow = this.processTemplate(field);
      rowList = this.processCombine(tempRowList, oneRow);
    } else {
      rowList.push(this.processTemplate(field));
    }
    //给每一行一个前端id
    rowList.forEach((row) => {
      row.frontOID = this.generateUid();
    });
    return rowList;
  },

  /**
   * 处理添加的时候需要的模板数据
   * @param field
   */
  processTemplate(field) {
    let children = [];
    let row = [];
    //控件有值的情况下从第一行提取模板，没有值的情况下从children里提取
    // if (field.showValue && JSON.parse(field.showValue) && JSON.parse(field.showValue).length) {
    //   row = JSON.parse(field.showValue)[0];
    //   row.forEach((item) => {
    //     item.showValue = "";
    //     item.value = "";
    //   });
    //   children = row;
    // } else {
    //   children = field.children && field.children.length ? JSON.parse(JSON.stringify(field.children)) : [];
    // }
    //新建，编辑的时候后台都会返回children，直接从children里获取即可
    children = field.children && field.children.length ? JSON.parse(JSON.stringify(field.children)) : [];
    return children;
  },

  /**
   * 获取可添加行数最大值
   * @param field
   */
  processRowMax(field) {
    let max = 50;
    if (field.fieldContent && JSON.parse(field.fieldContent)) {
      max = JSON.parse(field.fieldContent).row || 50;
    }
    return max;
  },

  //生成4位随机数
  s4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  },
  //生成uuid
  generateUid() {
    return (
      this.s4() +
      this.s4() +
      "-" +
      this.s4() +
      "-" +
      this.s4() +
      "-" +
      this.s4() +
      "-" +
      this.s4() +
      this.s4() +
      this.s4()
    );
  },

  /**
   * 比对两个fieldList有木有不一样的value
   * 理论上这两个fieldList是按顺序一一对应的，不用通过fieldOID进行查找比对，且value都是format后的都是字符串类型
   * @param newFieldList
   * @param oldFieldList
   */
  isDiffRow(newFieldList, oldFieldList) {
    let isDiff = false;
    newFieldList.forEach((newField, newFieldIndex) => {
      if (newField.value !== oldFieldList[newFieldIndex].value) {
        isDiff = true;
      }
    });
    return isDiff;
  },

  /**
   * 把模板oneRow里的field的属性，覆盖到数据rowList上
   * @param rowList
   * @param oneRow
   */
  processCombine(rowList, oneRow) {
    let resultList = [];
    let oneRowKeyMap = {};
    oneRow.forEach((field) => {
      oneRowKeyMap[field.fieldOID] = JSON.parse(JSON.stringify(field));
    });

    rowList.forEach((row) => {
      let tempOneRow = [];
      row.forEach((field) => {
        tempOneRow.push({
          ...oneRowKeyMap[field.fieldOID],
          ...field,
        });
      });
      resultList.push(tempOneRow);
    });

    return resultList;
  },
};
